<?php

namespace app\common\model\chat;

use addons\third\model\Third;
use app\common\library\Utils;
use app\common\model\User;
use think\Db;
use think\Model;

class ChatRoom extends Model
{
    protected $name = 'chat_room';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    //时间获取器
    public function getCreatetimeAttr($value, $data)
    {
        $day = Utils::time_format($value);
        return $day;
    }
    public function getStartTimeTextAttr($value, $data) {
        $day = Utils::time_format($data['start_time'], 'Y-m-d');
        $time = Utils::time_format($data['start_time'], 'H:i:s');
        return [$day, $time];
    }

    public function getTalkInfoAttr($value, $data) {
        $info = ChatUser::get($data['talk_id']);
        $user = Third::get($info['user_id']);
        return [
            'username' => $info['name'],
            'telphone'   => $info['telphone'],
            'user_id'   => $info['user_id'],
            'avatar'   => isset($user['avatar'])?$user['avatar']:''
        ];
    }

    public function getCourseInfoAttr($value, $data) {
        $info = ChatUser::get($data['talk_id']);
        return [
            'username' => $info['name'],
            'telphone'   => $info['telphone']
        ];
    }

    public function getStartEndAttr($value, $data) {
        $start = Utils::time_format($data['start_time'], 'H:i');
        $end = Utils::time_format($data['end_time'], 'H:i');
        return [$start, $end];
    }



}
